﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DTO;
using BUS;
using System.Collections;

namespace PTTK11_HuongRung
{
    public partial class frmThemThucDon : Form
    {
        ThucDonBUS tdBUS;
        ArrayList dsTD;

        public frmThemThucDon()
        {
            InitializeComponent();
        }

        private void LayThongTinThucDon(ref ThucDonDTO td)
        {
            td.NGAYAPDUNG = dtpThoiGian.Value;
            td.BUOIAPDUNG = cbbBuoi.Text.ToString();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ThucDonDTO td = new ThucDonDTO();
            td.NGAYAPDUNG = dtpThoiGian.Value;
            td.BUOIAPDUNG = cbbBuoi.Text.ToString();
            td.SOLUONG = dgvDanhSach.Rows.Count;
            ThucDonBUS tdon = new ThucDonBUS();
            tdon.ThemThucDon(td);

            int ma = tdon.GetThucDonID();
            ChiTietThucDonBUS ctdon = new ChiTietThucDonBUS();

            for (int i = 0; i < dgvDanhSach.Rows.Count; i++)
            {
                ChiTietThucDonDTO dto = new ChiTietThucDonDTO();
                dto.THUCDONID = ma;
                dto.MONANID = int.Parse(dgvDanhSach.Rows[i].Cells[1].Value.ToString());
                dto.SOLUONGPHANAN = int.Parse(txtSoluong.Text.Trim());
                ctdon.ThemThucdon(dto);
            }
            
            MessageBox.Show("Them THANH CONG!");
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            btnVao.Visible = false;
            btnDong.Visible = true;
            btnLuu.Visible = true;
            btnXoa.Visible = true;
            btnCapNhat.Enabled = false;
            btnThem.Enabled = false;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmThucDon frm = new frmThucDon();
            frm.ShowDialog();
            this.Close();
        }
        int row;
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (row < 0)
                return;
            dgvDanhSach.Rows[row].Cells[1].Value = cbbMonAn.SelectedValue;
            dgvDanhSach.Rows[row].Cells[2].Value = cbbMonAn.Text;
            dgvDanhSach.Rows[row].Cells[3].Value = txtSoluong.Text;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {

        }

        private void btnVao_Click(object sender, EventArgs e)
        {
            int i = dgvDanhSach.Rows.Count + 1;

            for (int j = 0; j < dgvDanhSach.Rows.Count; j++)
                if (cbbMonAn.Text.CompareTo(dgvDanhSach.Rows[j].Cells[2].Value.ToString()) == 0 && cbbMonAn.SelectedValue.ToString().CompareTo(dgvDanhSach.Rows[j].Cells[1].Value.ToString()) == 0)
                {
                    MessageBox.Show("Món ăn '" + cbbMonAn.Text.Trim() + "' đã có trong thực đơn.");
                    return;
                }
            dgvDanhSach.Rows.Add(i, cbbMonAn.SelectedValue, cbbMonAn.Text, txtSoluong.Text);
        }

        private void frmThemThucDon_Load(object sender, EventArgs e)
        {
            btnDangNhap.Text = "Đăng Xuất";
            NhanVienBUS nvBus = new NhanVienBUS();
            NhanVienDTO nvDto = new NhanVienDTO();
            nvDto = nvBus.GetThongTinNhanVien(Secssion.MaNV);
            txtHoTen.Text = nvDto.TENNHANVIEN.Trim();
            LoaiNhanVienBUS lnvbus = new LoaiNhanVienBUS();
            txtMaNV.Text = lnvbus.GetTenLoaiNV(nvDto.LOAINHANVIEN);
            reLoadMonAn();
        }
        private void reLoadMonAn()
        {
            MonAnBUS bus = new MonAnBUS();
            cbbMonAn.DataSource = bus.GetDSMonAn();
            cbbMonAn.DisplayMember = "TENMONAN";
            cbbMonAn.ValueMember = "MONANID";

        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (btnDangNhap.Text.Trim().CompareTo("Đăng Xuất") == 0)
            {
                btnDangNhap.Text = "Đăng Nhâp";
                Secssion.MaNV = "";
                Secssion.MaKhac = "";

                txtHoTen.Text = "";
                txtMaNV.Text = "";
                this.Hide();
                frmQuanLy frm = new frmQuanLy();
                frm.ShowDialog();
                this.Close();
            }
            else
            {
                this.Hide();
                frmDangNhap frm = new frmDangNhap();
                frm.ShowDialog();
                this.Close();
            }
        }
    }
}
